package com.ones.sdp.kit.log.util;

import cn.hutool.core.util.URLUtil;
import cn.hutool.extra.servlet.JakartaServletUtil;
import com.ones.sdp.kit.log.constant.LogTypeEnum;
import com.ones.sdp.kit.log.model.dto.OnesLogDto;
import jakarta.servlet.http.HttpServletRequest;
import lombok.experimental.UtilityClass;
import org.springframework.http.HttpHeaders;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 系统日志工具类
 *
 * @author L.cm
 */
@UtilityClass
public class LogKit {

	/**
	 * 获取日志信息
	 * @return
	 */
	public OnesLogDto getLog() {
		HttpServletRequest request = ((ServletRequestAttributes) Objects
			.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
		OnesLogDto log = new OnesLogDto();
		log.setType(LogTypeEnum.NORMAL.getType());
		log.setRequestUri(URLUtil.getPath(request.getRequestURI()));
		log.setMethod(request.getMethod());
		log.setRemoteAddr(JakartaServletUtil.getClientIP(request));
		log.setUserAgent(request.getHeader(HttpHeaders.USER_AGENT));
		log.setCreateTime(LocalDateTime.now());
		return log;
	}
}
